<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { getAttendanceList, getDepartment } from '@/api/reqapi'
import { useRouter } from 'vue-router';
const router = useRouter()
const arr = ref([])
const num = 0
const page = ref('1')
const pagesize = ref('10')
const deptID = ref('')
const total = ref(0)
const queryObj = {
  page: page.value,
  pagesize: pagesize.value,
  deptID: deptID.value
}
const tableData = ref([])
function flattenUserData(data: any[]): any[] {
  let result: any[] = []

  data.forEach((item) => {
    if (Array.isArray(item.attendanceRecord)) {
      item.attendanceRecord.forEach((record) => {
        // 创建一个新对象，包含出勤记录的所有信息
        let flattenedRecord = { ...record }

        // 如果出勤记录中没有某些外层对象的信息，则添加它们
        // 注意：这里我们假设外层对象和出勤记录对象有相同的字段时，以外层对象为准
        // 但在实际情况下，您可能需要根据实际情况来调整这个逻辑
        ;['id', 'username', 'workNumber', 'departmentName', 'mobile'].forEach(
          (key) => {
            if (!(key in flattenedRecord)) {
              flattenedRecord[key] = item[key]
            }
          }
        )

        // 将处理后的出勤记录对象添加到结果数组中
        result.push(flattenedRecord)
      })
    }
  })

  return result
}
const init = async () => {
  await getAttendanceList().then((res) => {
    total.value = res.data.total
    tableData.value = flattenUserData(res.data.data.rows)
    console.log(tableData.value)
  })
  await getAttendanceList().then((res) => {
    arr.value = res.data.data.rows
    console.log(arr.value)
  })
}

onMounted(() => {
  init()
})
</script>

<template>
  <div class="asa">
    <div class="num">
      <span>未处理</span>
      <span>{{ num }}</span>
    </div>
    <div class="r">
      <el-button @click="$router.push('/ditu')">打卡范围</el-button>
      <el-button type="primary">设置</el-button>
    </div>
  </div>

  <div class="white filter">
    部门：
    <!-- departmentName -->
    <ul v-for="(item, index) in arr" :key="item.id">
      <li style="list-style: none">
        <input type="checkbox" :id="item.id" />
        <span>{{ item.departmentName }}</span>
      </li>
    </ul>
  </div>
  <el-table :data="tableData" style="width: 100%; margin-bottom: 20px" border>
    <el-table-column label="序号" width="180">
      <template #default="scope">
        <span>{{ scope.$index + 1 }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="username" label="姓名" width="180" />
    <el-table-column prop="workNumber" label="工号" width="180" />
    <el-table-column prop="departmentName" label="部门" width="180" />
    <el-table-column prop="mobile" label="手机" width="180" />
    <el-table-column prop="adtStatu" label="考勤状态" width="180" />
    <el-table-column prop="day" label="日期" width="180" />
    <el-table-column prop="adtInTime" label="打卡时间" width="180" />
    <el-table-column prop="adtInPlace" label="打卡地点" width="180" />
    <el-table-column prop="adtOutTime" label="打卡时间" width="180" />
    <el-table-column prop="adtOutPlace" label="打卡地点" width="180" />
    <el-table-column prop="adtInRemark" label="备注" width="180" />
  </el-table>
</template>

<style lang="less" scoped>
.asa {
  background-color: white;
  .r {
    margin-top: 10px;
  }
  padding: 40px 20px;
  border-radius: 5px;
  display: flex;
  justify-content: space-between;
  .num {
    font-size: 20px;
    display: flex;
    align-items: center;
    :nth-child(1) {
      margin-bottom: 10px;
    }
    flex-direction: column;
  }
}
.filter {
  display: flex;
  flex-wrap: wrap;
  background-color: white;
  justify-self: start;
  align-items: first baseline;
  margin: 20px;
  padding: 20px;
  border-radius: 5px;
}
</style>
